package sz.szsmk.citizencard.ui.adapter;

import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.DzsmkInfoViewBinding;
import sz.szsmk.citizencard.model.network.bean.response.QueryAllCardFuncRes;
import sz.szsmk.citizencard.model.network.bean.response.SeleciUserAuthRes;
import sz.szsmk.citizencard.model.network.bean.response.ValidCardAuthorizationRes;
import sz.szsmk.citizencard.ui.dzsmkavtivity.ElecCitizenCardActivity;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2019
 * author: zdh
 * created on: 2019/1/10 11:06
 * description:
 */
public class DzsmkInfoAdapter extends RecyclerView.Adapter<DzsmkInfoAdapter.DzsmkInfoHolder> {

    private ElecCitizenCardActivity mContext;
    private List<QueryAllCardFuncRes.CardFunctionInforsBean> viewData;
    private List<SeleciUserAuthRes.AuthTypeListBean> authInfos;


    private BaseInterface baseInterface;
    private List<ValidCardAuthorizationRes.CardAuthrationInforsBean> XXnkAuth;

    public void setXXnkAuth(List<ValidCardAuthorizationRes.CardAuthrationInforsBean> XXnkAuth) {
        this.XXnkAuth = XXnkAuth;
    }

    public interface BaseInterface {
        public void onClick(View view, String url, String syslabl, int need_header);
    }


    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }


    public interface GhfInterface {
        public void onClick();
    }

    public void GhfOnClick(GhfInterface ghfInterface) {
        this.ghfInterface = ghfInterface;
    }

    private GhfInterface ghfInterface;

    public DzsmkInfoAdapter(ElecCitizenCardActivity elecCitizenCardActivity) {
        mContext = elecCitizenCardActivity;
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.dzsmk_info_view;
    }

    @Override
    public DzsmkInfoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return DzsmkInfoHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(DzsmkInfoHolder holder, int position) {
        loadItemData(position, holder.binding);
    }

    private void loadItemData(final int position, final DzsmkInfoViewBinding binding) {

        if (viewData != null) {
            binding.labelExpandChild.setText(viewData.get(position).getFuncName());
            binding.authTv.setText(viewData.get(position).getDescription());
            int id = viewData.get(position).getId();
            final String funcName = viewData.get(position).getFuncName();
            int isvalid = viewData.get(position).getIsValid();
            if (isvalid == 1) {
                binding.authRl.setVisibility(View.VISIBLE);
                binding.isAuthTv.setText("查看权益");
            } else {
                binding.authRl.setVisibility(View.GONE);
            }
            if (authInfos != null) {

                if (funcName.equals("公积金")) {
                    authType(authInfos.get(1), binding, isvalid);
                } else if (funcName.equals("社保")) {
                    authType(authInfos.get(0), binding, isvalid);
                } else if (funcName.equals("桂花分")) {
                    authType(authInfos.get(2), binding, isvalid);
                }
            }

            if (funcName.equals("桂花分")) {
                binding.isAuthTv.setTextColor(mContext.getResources().getColor(R.color.color_00AAFF));
                binding.dzsmkCv.setBackgroundResource(R.drawable.shape_bg_02aeff);
                binding.arrowAuth.setImageResource(R.drawable.arrow_blue);

                binding.dzIcon.setImageResource(R.drawable.icon_ghf);
            } else if (funcName.equals("公积金")) {
                binding.dzsmkCv.setBackgroundResource(R.drawable.shape_bg_02aeff);
                binding.isAuthTv.setTextColor(mContext.getResources().getColor(R.color.color_00AAFF));
                binding.arrowAuth.setImageResource(R.drawable.arrow_blue);
                binding.dzIcon.setImageResource(R.drawable.icon_gjj);
            } else if (funcName.equals("高龄卡")) {
                binding.isAuthTv.setTextColor(mContext.getResources().getColor(R.color.color_69D254));
                binding.dzsmkCv.setBackgroundResource(R.drawable.shape_bg_58ca55);
                binding.dzIcon.setImageResource(R.drawable.icon_default);
                binding.arrowAuth.setImageResource(R.drawable.below_arrow_green);
            } else if (funcName.equals("惠民休闲年卡")) {
                binding.isAuthTv.setTextColor(mContext.getResources().getColor(R.color.color_69D254));
                binding.dzsmkCv.setBackgroundResource(R.drawable.shape_bg_58ca55);
                binding.dzIcon.setImageResource(R.drawable.icon_hmxxnk);
                binding.arrowAuth.setImageResource(R.drawable.below_arrow_green);
            } else if (funcName.equals("教育卡")) {
                binding.dzIcon.setImageResource(R.drawable.icon_jyykt);
                binding.isAuthTv.setTextColor(mContext.getResources().getColor(R.color.color_69D254));
                binding.dzsmkCv.setBackgroundResource(R.drawable.shape_bg_58ca55);
                binding.arrowAuth.setImageResource(R.drawable.below_arrow_green);
            } else if (funcName.equals("劳模卡")) {
                binding.dzIcon.setImageResource(R.drawable.icon_lmk);
                binding.isAuthTv.setTextColor(mContext.getResources().getColor(R.color.color_69D254));
                binding.dzsmkCv.setBackgroundResource(R.drawable.shape_bg_58ca55);
                binding.arrowAuth.setImageResource(R.drawable.below_arrow_green);
            } else if (funcName.equals("苏州市社会保障·市民卡")) {
                binding.dzsmkCv.setBackgroundResource(R.drawable.shape_bg_58ca55);
                binding.dzIcon.setImageResource(R.drawable.icon_default);
                binding.arrowAuth.setImageResource(R.drawable.below_arrow_green);
                binding.isAuthTv.setTextColor(mContext.getResources().getColor(R.color.color_69D254));
//                binding.arrowAuth.setImageResource(R.drawable.arrow_blue);
            } else if (funcName.equals("体惠卡")) {
                binding.dzIcon.setImageResource(R.drawable.icon_hmxxnk);
                binding.isAuthTv.setTextColor(mContext.getResources().getColor(R.color.color_69D254));
                binding.dzsmkCv.setBackgroundResource(R.drawable.shape_bg_58ca55);
                binding.arrowAuth.setImageResource(R.drawable.below_arrow_green);
            } else if (funcName.equals("爱心卡")) {
                binding.dzIcon.setImageResource(R.drawable.icon_xxk);
                binding.isAuthTv.setTextColor(mContext.getResources().getColor(R.color.color_69D254));
                binding.dzsmkCv.setBackgroundResource(R.drawable.shape_bg_58ca55);
                binding.arrowAuth.setImageResource(R.drawable.below_arrow_green);
            } else if (funcName.equals("无偿献血爱心卡")) {
                binding.dzIcon.setImageResource(R.drawable.icon_xxk);
                binding.isAuthTv.setTextColor(mContext.getResources().getColor(R.color.color_69D254));
                binding.dzsmkCv.setBackgroundResource(R.drawable.shape_bg_58ca55);
                binding.arrowAuth.setImageResource(R.drawable.below_arrow_green);
            } else if (funcName.equals("优待卡")) {
                binding.dzIcon.setImageResource(R.drawable.icon_ydk);
                binding.isAuthTv.setTextColor(mContext.getResources().getColor(R.color.color_69D254));
                binding.dzsmkCv.setBackgroundResource(R.drawable.shape_bg_58ca55);
                binding.arrowAuth.setImageResource(R.drawable.below_arrow_green);
            } else if (funcName.equals("专有账户")) {
                binding.dzIcon.setImageResource(R.drawable.icon_zyzh);
                binding.isAuthTv.setTextColor(mContext.getResources().getColor(R.color.color_69D254));
                binding.dzsmkCv.setBackgroundResource(R.drawable.shape_bg_58ca55);
                binding.arrowAuth.setImageResource(R.drawable.below_arrow_green);
            } else if (funcName.equals("亲子娃娃卡")) {
                binding.dzIcon.setImageResource(R.drawable.icon_qzwwa);
                binding.isAuthTv.setTextColor(mContext.getResources().getColor(R.color.color_69D254));
                binding.dzsmkCv.setBackgroundResource(R.drawable.shape_bg_58ca55);
                binding.arrowAuth.setImageResource(R.drawable.below_arrow_green);
            } else if (funcName.equals("拥军卡")) {
                binding.dzIcon.setImageResource(R.drawable.icon_yjk);
                binding.isAuthTv.setTextColor(mContext.getResources().getColor(R.color.color_69D254));
                binding.dzsmkCv.setBackgroundResource(R.drawable.shape_bg_58ca55);
                binding.arrowAuth.setImageResource(R.drawable.below_arrow_green);
            } else if (funcName.equals("企业服务卡")) {
                binding.dzIcon.setImageResource(R.drawable.icon_qyfwk);
                binding.isAuthTv.setTextColor(mContext.getResources().getColor(R.color.color_69D254));
                binding.dzsmkCv.setBackgroundResource(R.drawable.shape_bg_58ca55);
                binding.arrowAuth.setImageResource(R.drawable.below_arrow_green);
            } else if (funcName.equals("园林年卡")) {
                binding.dzIcon.setImageResource(R.drawable.icon_ylnk);
                binding.isAuthTv.setTextColor(mContext.getResources().getColor(R.color.color_69D254));
                binding.dzsmkCv.setBackgroundResource(R.drawable.shape_bg_58ca55);
                binding.arrowAuth.setImageResource(R.drawable.below_arrow_green);
            } else if (funcName.equals("残疾人卡")) {
                binding.dzIcon.setImageResource(R.drawable.icon_cjrk);
                binding.isAuthTv.setTextColor(mContext.getResources().getColor(R.color.color_69D254));
                binding.dzsmkCv.setBackgroundResource(R.drawable.shape_bg_58ca55);
                binding.arrowAuth.setImageResource(R.drawable.below_arrow_green);
            } else if (funcName.equals("苏州教育E卡通")) {
                binding.dzIcon.setImageResource(R.drawable.icon_jyykt);
                binding.isAuthTv.setTextColor(mContext.getResources().getColor(R.color.color_69D254));
                binding.dzsmkCv.setBackgroundResource(R.drawable.shape_bg_58ca55);
            } else if (funcName.equals("图书借阅")) {
                binding.dzIcon.setImageResource(R.drawable.icon_tsjy);
                binding.isAuthTv.setTextColor(mContext.getResources().getColor(R.color.color_69D254));
                binding.dzsmkCv.setBackgroundResource(R.drawable.shape_bg_58ca55);
            } else {
                binding.dzIcon.setImageResource(R.drawable.icon_default);
                binding.isAuthTv.setTextColor(mContext.getResources().getColor(R.color.color_69D254));
                binding.dzsmkCv.setBackgroundResource(R.drawable.shape_bg_58ca55);
                binding.arrowAuth.setImageResource(R.drawable.below_arrow_green);
            }

            binding.dzsmkRl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
                    String funcName1 = viewData.get(position).getFuncName();
                    if (funcName1.equals("专有账户")) {
                        Bundle bundle = new Bundle();
                        bundle.putString("type", "1");
                        bundle.putString(Constant.ORIGIN, "04");
                        JumpUtils.jumptoNatvie(mContext, "routinecarddetail", 0, bundle);
                    }

                    if (funcName1.equals("桂花分")) {
                        SeleciUserAuthRes.AuthTypeListBean bean = authInfos.get(2);
                        String isAuth = bean.getIsAuth();
                        if (isAuth.equals("2")) {
                            Bundle bundle = new Bundle();
                            bundle.putInt("intotype", 1);
                            bundle.putString("choosetype", "ghf");
                            JumpUtils.jumptoNatvie(mContext, "RealNameActivity", 0, bundle);

                        } else if (isAuth.equals("1")) {
                            ghfInterface.onClick();
                        } else if (isAuth.equals("0")) {
                            Bundle bundle = new Bundle();
                            bundle.putString(Constant.TYPE, "ghf");
                            bundle.putString(Constant.ID_NAME, Constant.USER_NAME);
                            JumpUtils.jumptoNatvie(mContext, "applicationAuthorActivity", 0, bundle);
                        }
                    } else if (funcName1.equals("公积金")) {

                        SeleciUserAuthRes.AuthTypeListBean bean = authInfos.get(1);
                        String isAuth = bean.getIsAuth();
                        if (isAuth.equals("1")) {
                            //跳转公积金
                            Bundle bundle = new Bundle();
                            String path = Constant.BASE_URL + "app-front-end/#/fund/main?access_token=" + access_token;
                            bundle.putString("url", path);
//                            bundle.putString(Constant.CUST_NAME,Constant.ID_NAME);
                            JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);

                        } else if (isAuth.equals("0")) {
                            JumpToAuth("gjj");
                        } else {
                            Bundle bundle = new Bundle();
                            bundle.putInt("intotype", 1);
                            bundle.putString("choosetype", "gjj");
                            JumpUtils.jumptoNatvie(mContext, "RealNameActivity", 0, bundle);
                        }

                    } else if (funcName1.equals("社保")) {
                        SeleciUserAuthRes.AuthTypeListBean bean = authInfos.get(1);
                        String isAuth = bean.getIsAuth();
                        if (isAuth.equals("2")) {
                            Bundle bundle = new Bundle();
                            bundle.putInt("intotype", 1);
                            bundle.putString("choosetype", "sb");
                            JumpUtils.jumptoNatvie(mContext, "RealNameActivity", 0, bundle);
                        } else if (isAuth.equals("1")) {
                            //跳转社保
                            Bundle bundle = new Bundle();
                            String path = Constant.BASE_URL + "app-front-end/#/social/main?access_token=" + access_token;
                            bundle.putString("url", path);
                            JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);

                        } else if (isAuth.equals("0")) {
                            Bundle bundle = new Bundle();
                            bundle.putString(Constant.TYPE, "sb");

                            String custName = Constant.USER_NAME;
                            bundle.putString(Constant.ID_NAME, custName);
                            JumpUtils.jumptoNatvie(mContext, "applicationAuthorActivity", 0, bundle);
                        }
                    } else {
                        Bundle bundle = new Bundle();

//                        电子市民卡首页基本信息 跳转到卡分类详情页面问题
//                        跳转链接地址:https://newapptest.szsmk.com/app-front-end/#/cardService/detail?id=3。参数：id根据接口返回值变化
                        if (viewData.get(position).getId() != 0) {
                            String url = Constant.BASE_URL + "app-front-end/#/cardService/detail?id=" + viewData.get(position).getId() + "&isMain=1";
                            bundle.putString("url", url);
                            JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
                        }
                    }
                }
            });

            binding.isAuthTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if (funcName.equals("桂花分")) {
                        SeleciUserAuthRes.AuthTypeListBean bean = authInfos.get(2);
                        String isAuth = bean.getIsAuth();
                        if (isAuth.equals("1")) {
                            toSetView(binding.authTv);
                            binding.authTv.setText(viewData.get(position).getDescription());
                        } else if (isAuth.equals("0")) {
                            JumpToAuth("ghf");
                        } else {
                            Bundle bundle = new Bundle();
                            bundle.putInt("intotype", 1);
                            bundle.putString("choosetype", "ghf");
                            JumpUtils.jumptoNatvie(mContext, "RealNameActivity", 0, bundle);
                        }
                    }
//                    toSetView(){}
                    else if (funcName.equals("公积金")) {
                        SeleciUserAuthRes.AuthTypeListBean bean = authInfos.get(1);
                        String isAuth = bean.getIsAuth();
                        if (isAuth.equals("1")) {
                            toSetView(binding.authTv);
                        } else if (isAuth.equals("0")) {

                            JumpToAuth("gjj");
                        } else {

                            Bundle bundle = new Bundle();
                            bundle.putInt("intotype", 1);
                            bundle.putString("choosetype", "gjj");
                            JumpUtils.jumptoNatvie(mContext, "RealNameActivity", 0, bundle);
                        }
//                        }
                    } else if (funcName.equals("社保")) {
                        SeleciUserAuthRes.AuthTypeListBean bean = authInfos.get(0);
                        String isAuth = bean.getIsAuth();
                        if (isAuth.equals("1")) {
                            toSetView(binding.authTv);

                        } else if (isAuth.equals("0")) {
                            JumpToAuth("sb");
                        } else {
                            Bundle bundle = new Bundle();
                            bundle.putInt("intotype", 1);
                            bundle.putString("choosetype", "sb");
                            JumpUtils.jumptoNatvie(mContext, "RealNameActivity", 0, bundle);
                        }
                    } else {
                        toSetView(binding.authTv);
                        binding.authTv.setText(viewData.get(position).getDescription());
                    }

                }

                private void toSetView(View view) {
                    if (view.getVisibility() == View.GONE) {
                        view.setVisibility(View.VISIBLE);
                    } else {
                        view.setVisibility(View.GONE);
                    }
                }
            });
        }
    }

    private void JumpToAuth(String type) {

        Bundle bundle = new Bundle();
        bundle.putString(Constant.TYPE, type);
        String custName = Constant.ID_NAME;
        bundle.putString(Constant.ID_NAME, custName);
        JumpUtils.jumptoNatvie(mContext, "applicationAuthorActivity", 0, bundle);

    }

    private void authType(SeleciUserAuthRes.AuthTypeListBean authTypeListBean, DzsmkInfoViewBinding binding, int isvalid) {
        //3是桂花分
        String isAuth = authTypeListBean.getIsAuth();
//        if (authType.equals("3")) {
        //0未授权，1已授权，2未实名(L3认证)
        if ("2".equals(isAuth) || "0".equals(isAuth)) {
            //去实名
            binding.isAuthTv.setVisibility(View.VISIBLE);
            binding.isAuthTv.setText("未授权");
        } else {
            if (isvalid == 1) {
                binding.isAuthTv.setVisibility(View.VISIBLE);
                binding.isAuthTv.setText("查看权益");
            } else {
                binding.isAuthTv.setVisibility(View.GONE);
            }

        }

    }

    @Override
    public int getItemCount() {
        return null == viewData ? 0 : viewData.size();
    }

    public void setViewData(List<QueryAllCardFuncRes.CardFunctionInforsBean> viewData) {
        this.viewData = viewData;
    }

    public void setAuthInfos(List<SeleciUserAuthRes.AuthTypeListBean> authInfos) {
        this.authInfos = authInfos;
    }

    public static class DzsmkInfoHolder extends RecyclerView.ViewHolder {
        private DzsmkInfoViewBinding binding;

        public static DzsmkInfoHolder creat(ViewGroup parent) {
            DzsmkInfoViewBinding binding = DzsmkInfoViewBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new DzsmkInfoHolder(binding);
        }

        public DzsmkInfoHolder(DzsmkInfoViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
